﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.SqliteWrapper;
using ZilLion.Service.TaskPlatform.Domain.Entity;
using ZilLion.Service.TaskPlatform.Domain.Respository.Interface;

namespace ZilLion.Service.TaskPlatform.Domain.Respository.Sqlite
{
    public class SqliteTaskCommandRespository : SqliteRepository<TaskConmmandEntity>, ITaskCommandRespository
    {
        public SqliteTaskCommandRespository()
        {
            Context = new TaskRunnerSqliteContext();
        }

        public TaskConmmandEntity GetTaskConmmandById(string conmmandId)
        {
            return GetList("select * from [task_command] where commandid=@conmmandId", new {conmmandId})
                .FirstOrDefault();
        }

        //public async Task<string> GetNewGuidAsync()
        //{
        //    return await Task.Run(() =>
        //    {
        //        Thread.Sleep(5000);
        //        return $@"{Guid.NewGuid():N}_HashCode:{GetHashCode()}";
        //    });
        //}
        public IEnumerable<TaskConmmandEntity> GetAllTaskConmmandEntities(string nodeid = null)
        {
            return nodeid.IsNotNullOrEmpty()
                ? GetList("select * from [task_command] where nodeid=@nodeid", new {nodeid})
                : GetList("select * from [task_command] ");
        }

        IEnumerable<TaskConmmandEntity> ITaskCommandRespository.GetUnHandleTaskConmmandEntitiesAsync(string nodeid)
        {
            return
                GetList("select * from [task_command] where nodeid=@nodeid and commandstate=0 ",
                    new {nodeid});
        }

        public void SaveTaskConmmandEntity(TaskConmmandEntity command, UnitOfWork unit = null)
        {
            if (command.CommandId.IsNullOrEmpty())
                Add(command, unit);

            else
                Modify(command, unit);
        }
    }
}